﻿Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class AddReview
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_AddReview_Click(sender As Object, e As EventArgs) Handles btn_AddReview.Click

        'Make this more robust - if string is still empty after select case
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim ext As String = IO.Path.GetExtension(f_Picture.FileName)
        Dim newFileName As String = myGUID.ToString() + ext

        If (f_Picture.HasFile And isPicture(ext)) Then
            Dim picLocationOnHardDiskServer = Request.MapPath("Pictures") & "/" & newFileName
            f_Picture.SaveAs(picLocationOnHardDiskServer)

            'resizes an image to 100x100
            Dim img1 As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath("~/Pictures/") + newFileName)
            Dim bmp1 As System.Drawing.Image = img1.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
            bmp1.Save(MapPath("~/Thumbnails/") + newFileName)

        Else
            Response.Redirect("AddReview.aspx")
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("AppsDatabaseConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO AppsDatabase(AppName, DeveloperName, Type, Description, Review, UserRating,  AppId, Pictures, Thumbnails) VALUES (@f1, @f2, @f3, @f4, @f5, @f6, @F7, @F8, @F9)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_AppName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_DeveloperName.Text)
        cmd.Parameters.AddWithValue("@f3", rbl_Type.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Review.Text)
        cmd.Parameters.AddWithValue("@f6", ddl_UserRating.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", tb_AppId.Text)
        cmd.Parameters.AddWithValue("@f8", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f9", "Thumbnails/" & newFileName)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("Confirmation.aspx")
    End Sub

    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function

End Class